from flask import  Flask ,request ,jsonify, session
from flask import redirect

app = Flask(__name__)

app.secret_key = 'asd'

@app.route('/')
def hello_world():
    return 'hello world'


@app.route('/json', methods=['POST','GET'])
def json():
    try:
        json = request.get_json()
        get_name = json.get('name')
        get_age = json.get('age')
        if not all([get_age, get_name]):
            return jsonify(msg='缺少参数')
        get_age += 10
        print(json)

        return jsonify(name=get_name, age=get_age)
    except Exception as e:
        print(e)
        return jsonify(msg='出错')

# 登录
@app.route('/login',methods=['POST'])
def login():
    get_date = request.get_json()
    username = get_date.get('username')
    password = get_date.get('password')

    if not all([username, password]):
        return jsonify(msg='参数不完整')
    if username == 'asd123' and password =='asdasd':
        session['username'] = username
        return jsonify(msg='登录成功')
    else:
        return jsonify(msg='账号或密码错误')

@app.route('/session',methods=['GET'])
def check():
    username = session.get('username')
    if username is not None:
        return jsonify(username=username)
    else:
        return jsonify(msg="出错了，没有登录")

@app.route('/logout',methods=['GET'])
def logout():
    session.clear()
    return jsonify(msg='退出登录')


app.run(host='192.168.3.22',debug=True)